#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) {
  char *r;
  if (c == 0)
    return strchr(s, 0);
  r = 0;
  while (s = strchr(s, c))
    r = (char *)s++;
  return r;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    113d <strrchr+0x2d>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <strchr@plt>
mov    %rax,-0x8(%rbp)
jmp    117f <strrchr+0x6f>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1177 <strrchr+0x67>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,-0x20(%rbp)
jmp    1145 <strrchr+0x35>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrchr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %esi,%esi
je     1149 <strrchr+0x29>
mov    %esi,%r14d
xor    %eax,%eax
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
jne    1130 <strrchr+0x10>
jmp    1157 <strrchr+0x37>
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*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i8* @strchr(i8* %10, i32 0)
store i8* %11, i8** %3
br label %23
12: ; preds = %2
store i8* null, i8** %6
br label %13
13: ; preds = %18, %12
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = call i8* @strchr(i8* %14, i32 %15)
store i8* %16, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %4
store i8* %19, i8** %6
br label %13
21: ; preds = %13
%22 = load i8*, i8** %6
store i8* %22, i8** %3
br label %23
23: ; preds = %21, %9
%24 = load i8*, i8** %3
ret i8* %24
}

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