#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 *rtrimwhitespace(char *str) {
  unsigned char endloc = strlen(str) - 1;
  while (isspace(str[endloc]))
    endloc--;
  if (endloc <= 0) {
    str[0] = '\0';
  } else {
    str[endloc + 1] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<rtrimwhitespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %al,-0x9(%rbp)
mov    -0x8(%rbp),%rax
movzbl -0x9(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     1163 <rtrimwhitespace+0x43>
mov    -0x9(%rbp),%al
add    $0xff,%al
mov    %al,-0x9(%rbp)
jmp    113b <rtrimwhitespace+0x1b>
movzbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jg     117c <rtrimwhitespace+0x5c>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
jmp    118e <rtrimwhitespace+0x6e>
mov    -0x8(%rbp),%rax
movzbl -0x9(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrimwhitespace>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <strlen@plt>
add    $0xff,%al
xchg   %ax,%ax
movzbl %al,%ebx
movsbl (%r14,%rbx,1),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
mov    %ebx,%eax
add    $0xff,%al
test   %rcx,%rcx
jne    1130 <rtrimwhitespace+0x10>
lea    0x1(%rbx),%rcx
xor    %edx,%edx
cmp    $0xff,%al
cmovne %rcx,%rdx
movb   $0x0,(%r14,%rdx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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