#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 /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ *strchr(char *, char);
int strlen(char *);
char *dj_string_rstrchr(char *src, char x) {
  int i = strlen(src);
  printf("str length = %d,ch = %c\n", i, x);
  if (!(*src))
    return 0;
  while (src[i - 1]) {
    if (strchr(src + (i - 1), x) != NULL)
      return (src + (i - 1));
    else
      i--;
  }
  printf("str1 length = %d\n", i);
  return 0;
}


x86 assembly (clang, -O0):
<dj_string_rstrchr>:
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>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
movsbl -0x11(%rbp),%edx
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    117d <dj_string_rstrchr+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11fb <dj_string_rstrchr+0xcb>
jmp    1182 <dj_string_rstrchr+0x52>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11e2 <dj_string_rstrchr+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rdi
movsbl -0x11(%rbp),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11d4 <dj_string_rstrchr+0xa4>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11fb <dj_string_rstrchr+0xcb>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1182 <dj_string_rstrchr+0x52>
mov    -0x18(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dj_string_rstrchr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebp
lea    0xeb5(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmpb   $0x0,(%r15)
je     11a4 <dj_string_rstrchr+0x74>
movslq %ebp,%rax
lea    (%rax,%r15,1),%rbx
add    $0xffffffffffffffff,%rbx
movsbl %r14b,%r14d
xchg   %ax,%ax
cmpb   $0x0,(%rbx)
je     1192 <dj_string_rstrchr+0x62>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1040 <strchr@plt>
add    $0xffffffff,%ebp
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
je     1170 <dj_string_rstrchr+0x40>
add    $0x1,%rbx
jmp    11a4 <dj_string_rstrchr+0x74>
lea    0xe80(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"str length = %d,ch = %c\0A\00"
@.str.1 = [18 x i8] c"str1 length = %d\0A\00"
define i8* @dj_string_rstrchr(i8* %0, i8 signext %1) {
%3 = alloca i8*
%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)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = load i8, i8* %5
%11 = sext i8 %10 to i32
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %11)
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %2
store i8* null, i8** %3
br label %48
17: ; preds = %2
br label %18
18: ; preds = %44, %17
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 0
br i1 %25, label %26, label %45
26: ; preds = %18
%27 = load i8*, i8** %4
%28 = load i32, i32* %6
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i8, i8* %5
%33 = call i32* @strchr(i8* %31, i8 signext %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %41
35: ; preds = %26
%36 = load i8*, i8** %4
%37 = load i32, i32* %6
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8* %40, i8** %3
br label %48
41: ; preds = %26
%42 = load i32, i32* %6
%43 = add nsw i32 %42, -1
store i32 %43, i32* %6
br label %44
44: ; preds = %41
br label %18
45: ; preds = %18
%46 = load i32, i32* %6
%47 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %46)
store i8* null, i8** %3
br label %48
48: ; preds = %45, %35, %16
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [25 x i8] c"str length = %d,ch = %c\0A\00"
@.str.1 = [18 x i8] c"str1 length = %d\0A\00"
define i8* @dj_string_rstrchr(i8* %0, i8 signext %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = sext i8 %1 to i32
%5 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = sext i32 %3 to i64
br label %10
10: ; preds = %8, %16
%11 = phi i64 [ %9, %8 ], [ %12, %16 ]
%12 = add i64 %11, -1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = tail call i32* @strchr(i8* %13, i8 signext %1)
%18 = icmp eq i32* %17, null
br i1 %18, label %10, label %22
19: ; preds = %10
%20 = trunc i64 %11 to i32
%21 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %16, %2, %19
%23 = phi i8* [ null, %19 ], [ null, %2 ], [ %13, %16 ]
ret i8* %23
}