#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 */
size_t strlen(char const *);
int /*<<< orphan*/ strncmp(char const *, char const *, size_t);
const char *backwards_strstr(const char *haystack, const char *needle,
                             int offset) {
  size_t needle_len = strlen(needle);
  for (int i = offset; i >= 0; i--) {
    if (!strncmp(haystack + i, needle, needle_len)) {
      return haystack + i;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<backwards_strstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     1198 <backwards_strstr+0x78>
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1185 <backwards_strstr+0x65>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11a0 <backwards_strstr+0x80>
jmp    118a <backwards_strstr+0x6a>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1146 <backwards_strstr+0x26>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<backwards_strstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %ebx,%ebx
js     1172 <backwards_strstr+0x52>
mov    %rax,%r12
mov    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbx,1),%r13
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1175 <backwards_strstr+0x55>
lea    -0x1(%rbx),%rax
test   %rbx,%rbx
mov    %rax,%rbx
jg     1150 <backwards_strstr+0x30>
xor    %r13d,%r13d
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @backwards_strstr(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i32, i32* %7
store i32 %12, i32* %9
br label %13
13: ; preds = %31, %3
%14 = load i32, i32* %9
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i8*, i8** %5
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8*, i8** %6
%22 = load i64, i64* %8
%23 = call i32 @strncmp(i8* %20, i8* %21, i64 %22)
%24 = icmp ne i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %16
%26 = load i8*, i8** %5
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8* %29, i8** %4
br label %35
30: ; preds = %16
br label %31
31: ; preds = %30
%32 = load i32, i32* %9
%33 = add nsw i32 %32, -1
store i32 %33, i32* %9
br label %13
34: ; preds = %13
store i8* null, i8** %4
br label %35
35: ; preds = %34, %25
%36 = load i8*, i8** %4
ret i8* %36
}

LLVM IR (-O2):
define i8* @backwards_strstr(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp sgt i32 %2, -1
br i1 %5, label %6, label %16
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %11
8: ; preds = %11
%9 = add nsw i64 %12, -1
%10 = icmp sgt i64 %12, 0
br i1 %10, label %11, label %16
11: ; preds = %6, %8
%12 = phi i64 [ %7, %6 ], [ %9, %8 ]
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = tail call i32 @strncmp(i8* %13, i8* %1, i64 %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %11, %3
%17 = phi i8* [ null, %3 ], [ %13, %11 ], [ null, %8 ]
ret i8* %17
}