#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 *);
char *trailstr(char *str, char c) {
  size_t i = strlen(str) - 1;
  for (; i > 0 && str[i] == c; i--) {
    str[i] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<trailstr>:
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
cltq
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jbe    115c <trailstr+0x4c>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116c <trailstr+0x5c>
jmp    1189 <trailstr+0x79>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    1134 <trailstr+0x24>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trailstr>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
add    $0xffffffff,%eax
je     1140 <trailstr+0x30>
cltq
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %bpl,(%rbx,%rax,1)
jne    1140 <trailstr+0x30>
movb   $0x0,(%rbx,%rax,1)
add    $0xffffffffffffffff,%rax
jne    1130 <trailstr+0x20>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @trailstr(i8* returned %0, i8 signext %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, -1
%5 = icmp eq i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = sext i32 %4 to i64
br label %8
8: ; preds = %6, %13
%9 = phi i64 [ %14, %13 ], [ %7, %6 ]
%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 %16
13: ; preds = %8
store i8 0, i8* %10
%14 = add i64 %9, -1
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %8
16: ; preds = %8, %13, %2
ret i8* %0
}