#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*/ isspace(unsigned char);
size_t strlen(char *);
size_t rtrim_whitespace(unsigned char *str) {
  size_t i = strlen((char *)str);
  while (i) {
    i--;
    if (!isspace(str[i])) {
      i++;
      break;
    }
  }
  str[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<rtrim_whitespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1180 <rtrim_whitespace+0x60>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
jne    117b <rtrim_whitespace+0x5b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1180 <rtrim_whitespace+0x60>
jmp    1139 <rtrim_whitespace+0x19>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim_whitespace>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %rbx,%r15
sub    $0x1,%rbx
jb     1148 <rtrim_whitespace+0x28>
movzbl -0x1(%r14,%r15,1),%edi
call   1040 <isspace@plt>
test   %eax,%eax
jne    1130 <rtrim_whitespace+0x10>
movb   $0x0,(%r14,%r15,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @rtrim_whitespace(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 = %21, %1
%7 = load i64, i64* %3
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %22
9: ; preds = %6
%10 = load i64, i64* %3
%11 = add i64 %10, -1
store i64 %11, i64* %3
%12 = load i8*, i8** %2
%13 = load i64, i64* %3
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i8, i8* %14
%16 = call i32 @isspace(i8 zeroext %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %9
%19 = load i64, i64* %3
%20 = add i64 %19, 1
store i64 %20, i64* %3
br label %22
21: ; preds = %9
br label %6
22: ; preds = %18, %6
%23 = load i8*, i8** %2
%24 = load i64, i64* %3
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
%26 = load i64, i64* %3
ret i64 %26
}

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