#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 */
size_t strlen(char const *);
size_t trim_end_char(const char *data, char bad_char) {
  size_t len;
  len = strlen(data);
  if (len <= 0)
    return 0;
  while (*(data + len - 1) == bad_char) {
    len--;
  }
  return len;
}


x86 assembly (clang, -O0):
<trim_end_char>:
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    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
ja     1147 <trim_end_char+0x37>
movq   $0x0,-0x8(%rbp)
jmp    117e <trim_end_char+0x6e>
jmp    114c <trim_end_char+0x3c>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl -0x1(%rax,%rcx,1),%eax
movsbl -0x11(%rbp),%ecx
cmp    %ecx,%eax
jne    1176 <trim_end_char+0x66>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    114c <trim_end_char+0x3c>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trim_end_char>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     1140 <trim_end_char+0x30>
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
lea    -0x1(%rax),%rcx
cmp    %bpl,-0x2(%rbx,%rax,1)
mov    %rcx,%rax
je     1130 <trim_end_char+0x20>
jmp    1142 <trim_end_char+0x32>
xor    %ecx,%ecx
mov    %rcx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i64 @trim_end_char(i8* %0, i8 signext %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i64
store i8* %0, i8** %4
store i8 %1, i8* %5
%7 = load i8*, i8** %4
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %6
%9 = load i64, i64* %6
%10 = icmp ule i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i64 0, i64* %3
br label %28
12: ; preds = %2
br label %13
13: ; preds = %23, %12
%14 = load i8*, i8** %4
%15 = load i64, i64* %6
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -1
%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 %26
23: ; preds = %13
%24 = load i64, i64* %6
%25 = add i64 %24, -1
store i64 %25, i64* %6
br label %13
26: ; preds = %13
%27 = load i64, i64* %6
store i64 %27, i64* %3
br label %28
28: ; preds = %26, %11
%29 = load i64, i64* %3
ret i64 %29
}

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