#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 */
unsigned int strlen(char const *);
char *strrchr(const char *s, int c) {
  unsigned int i;
  unsigned int n = strlen(s);
  if (n == 0)
    return NULL;
  for (i = n - 1;; i--) {
    if (s[i] == c) {
      return (char *)&s[i];
    } else if (i == 0) {
      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>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1142 <strrchr+0x32>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <strrchr+0x91>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    -0x14(%rbp),%eax
jne    1172 <strrchr+0x62>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <strrchr+0x91>
cmpl   $0x0,-0x18(%rbp)
jne    1189 <strrchr+0x79>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <strrchr+0x91>
jmp    118e <strrchr+0x7e>
jmp    1193 <strrchr+0x83>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    114b <strrchr+0x3b>
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     1140 <strrchr+0x30>
mov    %eax,%ecx
add    $0xffffffffffffffff,%rcx
xor    %eax,%eax
nopl   0x0(%rax)
movsbl (%rbx,%rcx,1),%edx
cmp    %ebp,%edx
je     1144 <strrchr+0x34>
add    $0xffffffffffffffff,%rcx
jb     1130 <strrchr+0x20>
jmp    114c <strrchr+0x3c>
xor    %eax,%eax
jmp    114c <strrchr+0x3c>
mov    %ecx,%eax
add    %rax,%rbx
mov    %rbx,%rax
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
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* null, i8** %3
br label %39
13: ; preds = %2
%14 = load i32, i32* %7
%15 = sub i32 %14, 1
store i32 %15, i32* %6
br label %16
16: ; preds = %36, %13
%17 = load i8*, i8** %4
%18 = load i32, i32* %6
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = load i32, i32* %5
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %30
25: ; preds = %16
%26 = load i8*, i8** %4
%27 = load i32, i32* %6
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8* %29, i8** %3
br label %39
30: ; preds = %16
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i8* null, i8** %3
br label %39
34: ; preds = %30
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
%37 = load i32, i32* %6
%38 = add i32 %37, -1
store i32 %38, i32* %6
br label %16
39: ; preds = %33, %25, %12
%40 = load i8*, i8** %3
ret i8* %40
}

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 %19, label %5
5: ; preds = %2
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %5, %14
%8 = phi i64 [ %6, %5 ], [ %9, %14 ]
%9 = add nsw i64 %8, -1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, %1
br i1 %13, label %16, label %14
14: ; preds = %7
%15 = icmp eq i64 %9, 0
br i1 %15, label %19, label %7
16: ; preds = %7
%17 = and i64 %9, 4294967295
%18 = getelementptr inbounds i8, i8* %0, i64 %17
br label %19
19: ; preds = %14, %16, %2
%20 = phi i8* [ null, %2 ], [ %18, %16 ], [ null, %14 ]
ret i8* %20
}