#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*/ assert(char *);
int /*<<< orphan*/ isspace(char);
size_t strlen(char *);
void rtrim(char *s) {
  assert(s);
  size_t len = strlen(s);
  size_t r, i;
  if (!len) {
    return;
  }
  r = i = len;
  do {
    if (!isspace(s[--i])) {
      break;
    }
  } while ((r = i));
  if (r < len) {
    s[r] = 0;
  }
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1162 <rtrim+0x32>
jmp    11ca <rtrim+0x9a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
movsbl -0x1(%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
jne    1199 <rtrim+0x69>
jmp    11b0 <rtrim+0x80>
jmp    119e <rtrim+0x6e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    116e <rtrim+0x3e>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ca <rtrim+0x9a>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1050 <assert@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
test   %rax,%rax
je     1171 <rtrim+0x41>
mov    %rax,%r14
mov    %rax,%rbx
movsbl -0x1(%r15,%rbx,1),%edi
call   1040 <isspace@plt>
test   %eax,%eax
je     1167 <rtrim+0x37>
add    $0xffffffffffffffff,%rbx
jne    1150 <rtrim+0x20>
xor    %ebx,%ebx
cmp    %r14,%rbx
jae    1171 <rtrim+0x41>
movb   $0x0,(%r15,%rbx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @rtrim(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @assert(i8* %6)
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = icmp ne i64 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %1
br label %36
13: ; preds = %1
%14 = load i64, i64* %3
store i64 %14, i64* %5
store i64 %14, i64* %4
br label %15
15: ; preds = %25, %13
%16 = load i8*, i8** %2
%17 = load i64, i64* %5
%18 = add i64 %17, -1
store i64 %18, i64* %5
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = call i32 @isspace(i8 signext %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %15
br label %28
24: ; preds = %15
br label %25
25: ; preds = %24
%26 = load i64, i64* %5
store i64 %26, i64* %4
%27 = icmp ne i64 %26, 0
br i1 %27, label %15, label %28
28: ; preds = %25, %23
%29 = load i64, i64* %4
%30 = load i64, i64* %3
%31 = icmp ult i64 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i8*, i8** %2
%34 = load i64, i64* %4
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %12, %32, %28
ret void
}

LLVM IR (-O2):
define void @rtrim(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %19, label %5
5: ; preds = %1, %12
%6 = phi i64 [ %7, %12 ], [ %3, %1 ]
%7 = add i64 %6, -1
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = tail call i32 @isspace(i8 signext %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %5
%13 = icmp eq i64 %7, 0
br i1 %13, label %14, label %5
14: ; preds = %5, %12
%15 = phi i64 [ 0, %12 ], [ %6, %5 ]
%16 = icmp ult i64 %15, %3
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %18
br label %19
19: ; preds = %14, %17, %1
ret void
}