#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 *, char const *, size_t const);
char *tim_strrevstr(char *restrict haystack, unsigned off,
                    const char *restrict needle) {
  const size_t nlen = strlen(needle);
  for (char *p = haystack + off; p >= haystack; p--) {
    if (!strncmp(p, needle, nlen))
      return p;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<tim_strrevstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     1199 <tim_strrevstr+0x79>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1183 <tim_strrevstr+0x63>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <tim_strrevstr+0x81>
jmp    1188 <tim_strrevstr+0x68>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    114e <tim_strrevstr+0x2e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tim_strrevstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %ebx,%ebx
add    %r15,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
je     115d <tim_strrevstr+0x3d>
add    $0xffffffffffffffff,%rbx
cmp    %r15,%rbx
jae    1140 <tim_strrevstr+0x20>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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