#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 */
long strcspn(char *, char const *);
long strspn(char *, char const *);
void str_trim_tail(char *str, const char *spc_char) {
  long idx, next_idx;
  long i;
  idx = 0;
  for (i = 0; i < 10240; i++) {
    next_idx = idx + strcspn(&(str[idx]), spc_char);
    if (str[next_idx] == '\0') {
      if (i == 0) {
        return;
      } else {
        break;
      }
    }
    idx = next_idx;
    next_idx = idx + strspn(&(str[idx]), spc_char);
    if (str[next_idx] == '\0')
      break;
    idx = next_idx;
  }
  str[idx] = '\0';
}


x86 assembly (clang, -O0):
<str_trim_tail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x2800,-0x28(%rbp)
jge    1201 <str_trim_tail+0xe1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcspn@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    119f <str_trim_tail+0x7f>
cmpq   $0x0,-0x28(%rbp)
jne    119a <str_trim_tail+0x7a>
jmp    120d <str_trim_tail+0xed>
jmp    1201 <str_trim_tail+0xe1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strspn@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11e8 <str_trim_tail+0xc8>
jmp    1201 <str_trim_tail+0xe1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1140 <str_trim_tail+0x20>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_trim_tail>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %r13d,%r13d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r15,%r12,1),%rdi
mov    %r14,%rsi
call   1040 <strcspn@plt>
mov    %rax,%rbx
add    %r12,%rbx
cmpb   $0x0,(%r15,%rbx,1)
je     1184 <str_trim_tail+0x64>
lea    (%r15,%rbx,1),%rdi
mov    %r14,%rsi
call   1030 <strspn@plt>
mov    %rax,%r12
add    %rbx,%r12
cmpb   $0x0,(%r15,%r12,1)
je     118c <str_trim_tail+0x6c>
add    $0xffffffffffffffff,%r13
mov    %r12,%rbx
cmp    $0xffffffffffffd800,%r13
jne    1140 <str_trim_tail+0x20>
jmp    118c <str_trim_tail+0x6c>
mov    %r12,%rbx
test   %r13,%r13
je     1191 <str_trim_tail+0x71>
movb   $0x0,(%r15,%rbx,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @str_trim_tail(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
store i64 0, i64* %5
store i64 0, i64* %7
br label %8
8: ; preds = %48, %2
%9 = load i64, i64* %7
%10 = icmp slt i64 %9, 10240
br i1 %10, label %11, label %51
11: ; preds = %8
%12 = load i64, i64* %5
%13 = load i8*, i8** %3
%14 = load i64, i64* %5
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i8*, i8** %4
%17 = call i64 @strcspn(i8* %15, i8* %16)
%18 = add nsw i64 %12, %17
store i64 %18, i64* %6
%19 = load i8*, i8** %3
%20 = load i64, i64* %6
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %11
%26 = load i64, i64* %7
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
br label %55
29: ; preds = %25
br label %51
30: ; preds = %11
%31 = load i64, i64* %6
store i64 %31, i64* %5
%32 = load i64, i64* %5
%33 = load i8*, i8** %3
%34 = load i64, i64* %5
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = load i8*, i8** %4
%37 = call i64 @strspn(i8* %35, i8* %36)
%38 = add nsw i64 %32, %37
store i64 %38, i64* %6
%39 = load i8*, i8** %3
%40 = load i64, i64* %6
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %30
br label %51
46: ; preds = %30
%47 = load i64, i64* %6
store i64 %47, i64* %5
br label %48
48: ; preds = %46
%49 = load i64, i64* %7
%50 = add nsw i64 %49, 1
store i64 %50, i64* %7
br label %8
51: ; preds = %45, %29, %8
%52 = load i8*, i8** %3
%53 = load i64, i64* %5
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8 0, i8* %54
br label %55
55: ; preds = %51, %28
ret void
}

LLVM IR (-O2):
define void @str_trim_tail(i8* nocapture %0, i8* nocapture %1) {
br label %3
3: ; preds = %2, %20
%4 = phi i64 [ 0, %2 ], [ %21, %20 ]
%5 = phi i64 [ 0, %2 ], [ %16, %20 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = tail call i64 @strcspn(i8* %6, i8* %1)
%8 = add nsw i64 %7, %5
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = icmp eq i64 %4, 0
br i1 %13, label %26, label %23
14: ; preds = %3
%15 = tail call i64 @strspn(i8* %9, i8* %1)
%16 = add nsw i64 %15, %8
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %14
%21 = add nuw nsw i64 %4, 1
%22 = icmp eq i64 %21, 10240
br i1 %22, label %23, label %3
23: ; preds = %14, %20, %12
%24 = phi i64 [ %5, %12 ], [ %16, %20 ], [ %8, %14 ]
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %12, %23
ret void
}