#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 */
void *_memrchr(const void *buf, unsigned char c, size_t length) {
  unsigned char *p = (unsigned char *)buf;
  if (length == 0) {
    return NULL;
  }
  for (;;) {
    if (*p-- == c) {
      break;
    }
    if (length-- == 0) {
      return NULL;
    }
  }
  return (void *)(p + 1);
}


x86 assembly (clang, -O0):
<_memrchr>:
push   %rbp
mov    %rsp,%rbp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1132 <_memrchr+0x32>
movq   $0x0,-0x8(%rbp)
jmp    1191 <_memrchr+0x91>
jmp    1137 <_memrchr+0x37>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
movzbl (%rax),%eax
movzbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jne    115a <_memrchr+0x5a>
jmp    1185 <_memrchr+0x85>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
jne    1180 <_memrchr+0x80>
movq   $0x0,-0x8(%rbp)
jmp    1191 <_memrchr+0x91>
jmp    1137 <_memrchr+0x37>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_memrchr>:
test   %rdx,%rdx
je     111f <_memrchr+0x1f>
mov    %rdi,%rax
add    $0x1,%rdx
nopl   0x0(%rax)
cmp    %sil,(%rax)
je     1121 <_memrchr+0x21>
add    $0xffffffffffffffff,%rax
add    $0xffffffffffffffff,%rdx
jne    1110 <_memrchr+0x10>
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i8* @_memrchr(i8* %0, i8 zeroext %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %14, label %5
5: ; preds = %3, %10
%6 = phi i64 [ %12, %10 ], [ %2, %3 ]
%7 = phi i8* [ %11, %10 ], [ %0, %3 ]
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, %1
br i1 %9, label %14, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %7, i64 -1
%12 = add i64 %6, -1
%13 = icmp eq i64 %6, 0
br i1 %13, label %14, label %5
14: ; preds = %5, %10, %3
%15 = phi i8* [ null, %3 ], [ %7, %5 ], [ null, %10 ]
ret i8* %15
}