#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 *strchr(char const *, int);
char *strrchr(const char *s, int c) {
  const char *found, *p;
  c = (unsigned char)c;
  if (c == '\0')
    return strchr(s, '\0');
  found = NULL;
  while ((p = strchr(s, c)) != NULL) {
    found = p;
    s = p + 1;
  }
  return (char *)found;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1146 <strrchr+0x36>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
mov    %rax,-0x8(%rbp)
jmp    1189 <strrchr+0x79>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1181 <strrchr+0x71>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114e <strrchr+0x3e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrchr>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
and    $0xff,%r14d
je     1159 <strrchr+0x39>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
jne    1140 <strrchr+0x20>
jmp    1167 <strrchr+0x47>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
add    %rbx,%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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