#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 strlen(char *);
char *strstr(char *, char *);
char *strstr_fromright(char *pString, char *pSearch) {
  int c;
  int position = strlen(pSearch);
  if (position >= 0) {
    for (position--; position >= 0; position--) {
      char *pFound = strstr(&pString[position], pSearch);
      if (pFound == &pString[position])
        return pFound;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strstr_fromright>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11ae <strstr_fromright+0x8e>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11a9 <strstr_fromright+0x89>
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jne    1196 <strstr_fromright+0x76>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <strstr_fromright+0x96>
jmp    119b <strstr_fromright+0x7b>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    114f <strstr_fromright+0x2f>
jmp    11ae <strstr_fromright+0x8e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strstr_fromright>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
js     116b <strstr_fromright+0x4b>
mov    %eax,%r12d
nopl   (%rax)
test   %r12,%r12
jle    116b <strstr_fromright+0x4b>
add    $0xffffffffffffffff,%r12
mov    %r12d,%ebx
add    %r14,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1140 <strstr_fromright+0x20>
jmp    116d <strstr_fromright+0x4d>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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