#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 */
scalar_t__ isspace(unsigned char);
size_t strlen(char *);
void rtrim(char *str) {
  size_t n;
  n = strlen(str);
  while (n > 0 && (isspace((unsigned char)str[n - 1]) ||
                   (unsigned char)str[n - 1] == ' ')) {
    n--;
  }
  str[n] = '\0';
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x11(%rbp)
jbe    118f <rtrim+0x6f>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x12(%rbp)
jne    1189 <rtrim+0x69>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
sete   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    119f <rtrim+0x7f>
jmp    11b0 <rtrim+0x90>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1139 <rtrim+0x19>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %rax,%rax
je     1160 <rtrim+0x40>
mov    %rax,%rbx
jmp    1146 <rtrim+0x26>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
je     1160 <rtrim+0x40>
movzbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
jne    1140 <rtrim+0x20>
cmpb   $0x20,-0x1(%r14,%rbx,1)
je     1140 <rtrim+0x20>
jmp    1162 <rtrim+0x42>
xor    %ebx,%ebx
movb   $0x0,(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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