#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 *mem_begin(char *);
int /*<<< orphan*/ *strchr(char *, char);
char *str_rfind_char(char *haystack, char *needles) {
  char *haystack_begin = mem_begin(haystack);
  char *str = NULL;
  if (haystack == NULL)
    return (NULL);
  if (needles == NULL)
    return (NULL);
  for (str = haystack; str >= haystack_begin; str--) {
    if (strchr(needles, *str) != NULL)
      return (str);
  }
  return (NULL);
}


x86 assembly (clang, -O0):
<str_rfind_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <mem_begin@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115d <str_rfind_char+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    11d0 <str_rfind_char+0xb0>
cmpq   $0x0,-0x18(%rbp)
jne    1175 <str_rfind_char+0x55>
movq   $0x0,-0x8(%rbp)
jmp    11d0 <str_rfind_char+0xb0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11c8 <str_rfind_char+0xa8>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11b2 <str_rfind_char+0x92>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d0 <str_rfind_char+0xb0>
jmp    11b7 <str_rfind_char+0x97>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    117d <str_rfind_char+0x5d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_rfind_char>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <mem_begin@plt>
xor    %r14d,%r14d
test   %rbx,%rbx
je     116e <str_rfind_char+0x4e>
test   %r15,%r15
je     116e <str_rfind_char+0x4e>
mov    %rax,%r12
cmp    %rbx,%rax
ja     116e <str_rfind_char+0x4e>
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
movsbl (%rbx),%esi
mov    %r15,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
jne    116b <str_rfind_char+0x4b>
add    $0xffffffffffffffff,%rbx
cmp    %r12,%rbx
jae    1150 <str_rfind_char+0x30>
jmp    116e <str_rfind_char+0x4e>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @str_rfind_char(i8* %0, i8* %1) {
%3 = tail call i8* @mem_begin(i8* %0)
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
%7 = icmp ugt i8* %3, %0
%8 = select i1 %6, i1 true, i1 %7
br i1 %8, label %17, label %9
9: ; preds = %2, %14
%10 = phi i8* [ %15, %14 ], [ %0, %2 ]
%11 = load i8, i8* %10
%12 = tail call i32* @strchr(i8* %1, i8 signext %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = getelementptr inbounds i8, i8* %10, i64 -1
%16 = icmp ult i8* %15, %3
br i1 %16, label %17, label %9
17: ; preds = %9, %14, %2
%18 = phi i8* [ null, %2 ], [ %10, %9 ], [ null, %14 ]
ret i8* %18
}