#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 *);
void strip_trailing_chars(char *str, char char_to_strip) {
  for (int i = strlen(str) - 1; i >= 0; i--) {
    if (str[i] == char_to_strip) {
      str[i] = '\0';
    } else {
      break;
    }
  }
}


x86 assembly (clang, -O0):
<strip_trailing_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%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)
cmpl   $0x0,-0x10(%rbp)
jl     117c <strip_trailing_chars+0x6c>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    1164 <strip_trailing_chars+0x54>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1169 <strip_trailing_chars+0x59>
jmp    117c <strip_trailing_chars+0x6c>
jmp    116e <strip_trailing_chars+0x5e>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1131 <strip_trailing_chars+0x21>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_trailing_chars>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    1147 <strip_trailing_chars+0x37>
mov    %eax,%eax
add    $0x1,%rax
nopw   0x0(%rax,%rax,1)
lea    -0x2(%rax),%ecx
cmp    %bpl,(%rbx,%rcx,1)
jne    1147 <strip_trailing_chars+0x37>
movb   $0x0,(%rbx,%rcx,1)
add    $0xffffffffffffffff,%rax
cmp    $0x1,%rax
ja     1130 <strip_trailing_chars+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @strip_trailing_chars(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 i32 @strlen(i8* %6)
%8 = sub nsw i32 %7, 1
store i32 %8, i32* %5
br label %9
9: ; preds = %29, %2
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, 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 i1 %21, label %22, label %27
22: ; preds = %12
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
br label %28
27: ; preds = %12
br label %32
28: ; preds = %22
br label %29
29: ; preds = %28
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
br label %9
32: ; preds = %27, %9
ret void
}

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