#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 *s, int c) {
  unsigned int i;
  if (strlen(s) == 0)
    return NULL;
  for (i = strlen(s) - 1; i != 0; i--) {
    if (s[i] == c)
      return (char *)&s[i];
  }
  return NULL;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    113e <strrchr+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1199 <strrchr+0x89>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1191 <strrchr+0x81>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    -0x14(%rbp),%eax
jne    117e <strrchr+0x6e>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1199 <strrchr+0x89>
jmp    1183 <strrchr+0x73>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    114d <strrchr+0x3d>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%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
je     115c <strrchr+0x4c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
xor    %eax,%eax
mov    %ecx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
sub    $0x1,%edx
jb     115e <strrchr+0x4e>
mov    %ecx,%ecx
movsbl (%rbx,%rcx,1),%esi
mov    %edx,%ecx
cmp    %ebp,%esi
jne    1140 <strrchr+0x30>
add    $0x1,%edx
add    %rdx,%rbx
mov    %rbx,%rax
jmp    115e <strrchr+0x4e>
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 i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %37
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
store i32 %14, i32* %6
br label %15
15: ; preds = %33, %11
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %36
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = load i32, i32* %5
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i8*, i8** %4
%29 = load i32, i32* %6
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8* %31, i8** %3
br label %37
32: ; preds = %18
br label %33
33: ; preds = %32
%34 = load i32, i32* %6
%35 = add i32 %34, -1
store i32 %35, i32* %6
br label %15
36: ; preds = %15
store i8* null, i8** %3
br label %37
37: ; preds = %36, %27, %10
%38 = load i8*, i8** %3
ret i8* %38
}

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