#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(char);
int strlen(char *);
char *rtrim(char *str) {
  int i = strlen(str) - 1;
  while ((isspace(str[i])) && (i >= 0))
    str[i--] = '\0';
  return str;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xd(%rbp)
je     1168 <rtrim+0x48>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1178 <rtrim+0x58>
jmp    1193 <rtrim+0x73>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    113b <rtrim+0x1b>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebp
movslq %eax,%rbx
movsbl -0x1(%rbx,%r14,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1182 <rtrim+0x62>
test   %ebp,%ebp
jle    1182 <rtrim+0x62>
lea    (%rbx,%r14,1),%rax
add    $0xffffffffffffffff,%rax
mov    %ebp,%ebp
lea    -0x2(%r14),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x0,(%rax)
movsbl (%rbx,%rbp,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1182 <rtrim+0x62>
lea    (%rbx,%rbp,1),%rax
lea    -0x1(%rbp),%rcx
cmp    $0x1,%rbp
mov    %rcx,%rbp
jne    1160 <rtrim+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @rtrim(i8* returned %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = tail call i64 @isspace(i8 signext %6)
%8 = icmp ne i64 %7, 0
%9 = icmp sgt i32 %2, 0
%10 = select i1 %8, i1 %9, i1 false
br i1 %10, label %11, label %24
11: ; preds = %1
%12 = zext i32 %2 to i64
%13 = add nsw i64 %12, -1
br label %14
14: ; preds = %11, %14
%15 = phi i64 [ %13, %11 ], [ %17, %14 ]
%16 = phi i8* [ %5, %11 ], [ %18, %14 ]
store i8 0, i8* %16
%17 = add nsw i64 %15, -1
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = tail call i64 @isspace(i8 signext %19)
%21 = icmp ne i64 %20, 0
%22 = icmp ne i64 %15, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %14, label %24
24: ; preds = %14, %1
ret i8* %0
}