#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 /*<<< orphan*/ *strchr(char const *, int);
char *strrchr(char const *, int);
char *escape_strrchr(const char *haystack, int c, const char *toescape) {
  char *p;
  if (strchr(toescape, c) == NULL)
    return strrchr(haystack, c);
  if ((p = strrchr(haystack, c)) == NULL)
    return NULL;
  if (p == haystack)
    return p;
  if (*(p - 1) != c)
    return p;
  p -= 2;
  return escape_strrchr(p, c, toescape);
}


x86 assembly (clang, -O0):
<escape_strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    116e <escape_strrchr+0x3e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strrchr@plt>
mov    %rax,-0x8(%rbp)
jmp    11ee <escape_strrchr+0xbe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1195 <escape_strrchr+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11ee <escape_strrchr+0xbe>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11b0 <escape_strrchr+0x80>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <escape_strrchr+0xbe>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    -0x14(%rbp),%eax
je     11ce <escape_strrchr+0x9e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ee <escape_strrchr+0xbe>
mov    -0x28(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <escape_strrchr@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_strrchr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     117d <escape_strrchr+0x5d>
nopl   (%rax)
mov    %rbp,%rdi
mov    %r15d,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1195 <escape_strrchr+0x75>
mov    %rax,%rbx
cmp    %rbp,%rax
je     1197 <escape_strrchr+0x77>
movsbl -0x1(%rbx),%eax
mov    %rbx,%rbp
cmp    %r15d,%eax
jne    1197 <escape_strrchr+0x77>
add    $0xfffffffffffffffe,%rbx
mov    %r14,%rdi
mov    %r15d,%esi
call   1030 <strchr@plt>
mov    %rbx,%rbp
test   %rax,%rax
jne    1140 <escape_strrchr+0x20>
jmp    1180 <escape_strrchr+0x60>
mov    %rbp,%rbx
mov    %rbx,%rdi
mov    %r15d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strrchr@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @escape_strrchr(i8* %0, i32 %1, i8* %2) {
%4 = tail call i32* @strchr(i8* %2, i32 %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %20, %3
%7 = phi i8* [ %0, %3 ], [ %21, %20 ]
%8 = tail call i8* @strrchr(i8* dereferenceable(1) %7, i32 %1)
br label %24
9: ; preds = %3, %20
%10 = phi i8* [ %21, %20 ], [ %0, %3 ]
%11 = tail call i8* @strrchr(i8* dereferenceable(1) %10, i32 %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %9
%14 = icmp eq i8* %11, %10
br i1 %14, label %24, label %15
15: ; preds = %13
%16 = getelementptr inbounds i8, i8* %11, i64 -1
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, %1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %11, i64 -2
%22 = tail call i32* @strchr(i8* %2, i32 %1)
%23 = icmp eq i32* %22, null
br i1 %23, label %6, label %9
24: ; preds = %15, %13, %9, %6
%25 = phi i8* [ %8, %6 ], [ %11, %15 ], [ %10, %13 ], [ null, %9 ]
ret i8* %25
}