#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;
  if (!str)
    return 0;
  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    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1144 <rtrim_whitespace+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11ac <rtrim_whitespace+0x8c>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1198 <rtrim_whitespace+0x78>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%eax
jne    1193 <rtrim_whitespace+0x73>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1198 <rtrim_whitespace+0x78>
jmp    1151 <rtrim_whitespace+0x31>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim_whitespace>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     115f <rtrim_whitespace+0x3f>
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%r15
sub    $0x1,%rbx
jb     1158 <rtrim_whitespace+0x38>
movzbl -0x1(%r14,%r15,1),%edi
call   1040 <isspace@plt>
test   %eax,%eax
jne    1140 <rtrim_whitespace+0x20>
movb   $0x0,(%r14,%r15,1)
jmp    1162 <rtrim_whitespace+0x42>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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