#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 strlen(char const *);
char *strrchr(const char *string, int c) {
  char *from = (char *)string + strlen(string);
  while (from >= string) {
    if (*from == c) {
      return from;
    }
    from--;
  }
  return (char *)0;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     117c <strrchr+0x6c>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    -0x14(%rbp),%eax
jne    116b <strrchr+0x5b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1184 <strrchr+0x74>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    1140 <strrchr+0x30>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrchr>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
js     1140 <strrchr+0x30>
mov    %eax,%eax
add    %rbx,%rax
cs nopw 0x0(%rax,%rax,1)
movsbl (%rax),%ecx
cmp    %ebp,%ecx
je     1142 <strrchr+0x32>
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jae    1130 <strrchr+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
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 i8*, i8** %4
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
store i8* %11, i8** %6
br label %12
12: ; preds = %24, %2
%13 = load i8*, i8** %6
%14 = load i8*, i8** %4
%15 = icmp uge i8* %13, %14
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i8*, i8** %6
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = load i32, i32* %5
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = load i8*, i8** %6
store i8* %23, i8** %3
br label %28
24: ; preds = %16
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %6
br label %12
27: ; preds = %12
store i8* null, i8** %3
br label %28
28: ; preds = %27, %22
%29 = load i8*, i8** %3
ret i8* %29
}

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