#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 */
scalar_t__ strlen(char *);
int rfind(char *string, char ch) {
  int last = (int)strlen(string) - 1;
  while (last >= 0 && string[last] != ch) {
    last--;
  }
  return last;
}


x86 assembly (clang, -O0):
<rfind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jl     1158 <rfind+0x48>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1168 <rfind+0x58>
jmp    1176 <rfind+0x66>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1131 <rfind+0x21>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rfind>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    %ecx,%edx
xor    %eax,%eax
test   %ecx,%ecx
cmovs  %ecx,%eax
add    $0xffffffff,%eax
nopl   0x0(%rax)
test   %edx,%edx
jle    1142 <rfind+0x32>
add    $0xffffffffffffffff,%rdx
mov    %edx,%ecx
cmp    %bpl,(%rbx,%rcx,1)
jne    1130 <rfind+0x20>
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rfind(i8* %0, i8 signext %1) {
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
store i8* %0, i8** %3
store i8 %1, i8* %4
%6 = load i8*, i8** %3
%7 = call i64 @strlen(i8* %6)
%8 = trunc i64 %7 to i32
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %5
br label %10
10: ; preds = %25, %2
%11 = load i32, i32* %5
%12 = icmp sge i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = load i8*, i8** %3
%15 = load i32, i32* %5
%16 = sext 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* %4
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %19, %21
br label %23
23: ; preds = %13, %10
%24 = phi i1 [ false, %10 ], [ %22, %13 ]
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* %5
%27 = add nsw i32 %26, -1
store i32 %27, i32* %5
br label %10
28: ; preds = %23
%29 = load i32, i32* %5
ret i32 %29
}

LLVM IR (-O2):
define i32 @rfind(i8* nocapture %0, i8 signext %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = trunc i64 %3 to i32
%5 = and i64 %3, 4294967295
%6 = call i32 @llvm.smin.i32(i32 %4, i32 0)
%7 = add i32 %6, -1
br label %8
8: ; preds = %13, %2
%9 = phi i64 [ %10, %13 ], [ %5, %2 ]
%10 = add nsw i64 %9, -1
%11 = trunc i64 %9 to i32
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = and i64 %10, 4294967295
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, %1
br i1 %17, label %18, label %8
18: ; preds = %13
%19 = trunc i64 %10 to i32
br label %20
20: ; preds = %8, %18
%21 = phi i32 [ %19, %18 ], [ %7, %8 ]
ret i32 %21
}