#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 *);
unsigned strrchr(char s[], char f) {
  unsigned i;
  for (i = strlen(s) - 1; i; i--) {
    if (s[i] == f)
      return i;
  }
  return -1;
}


x86 assembly (clang, -O0):
<strrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1170 <strrchr+0x60>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jne    115d <strrchr+0x4d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1177 <strrchr+0x67>
jmp    1162 <strrchr+0x52>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1131 <strrchr+0x21>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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>
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
sub    $0x1,%eax
jb     1144 <strrchr+0x34>
mov    %ecx,%edx
mov    %eax,%ecx
cmp    %bpl,(%rbx,%rdx,1)
jne    1130 <strrchr+0x20>
add    $0x1,%eax
jmp    1149 <strrchr+0x39>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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