#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 const *);
scalar_t__ strlenzero(char const *);
extern char *rtrim(const char *str) {
  int len;
  char *cur = (char *)str;
  if (strlenzero(str)) {
    return (cur);
  }
  len = strlen(str) - 1;
  while (len && isspace(cur[len])) {
    cur[len] = '\0';
    len--;
  }
  return (cur);
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlenzero@plt>
cmp    $0x0,%rax
je     1164 <rtrim+0x34>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cf <rtrim+0x9f>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
je     119d <rtrim+0x6d>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11ad <rtrim+0x7d>
jmp    11c7 <rtrim+0x97>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1173 <rtrim+0x43>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strlenzero@plt>
test   %rax,%rax
je     1154 <rtrim+0x24>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
je     1145 <rtrim+0x15>
movslq %ecx,%r12
add    %r14,%r12
mov    $0x1,%r15d
sub    %eax,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl (%r12,%rbx,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1145 <rtrim+0x15>
movb   $0x0,(%r12,%rbx,1)
add    $0xffffffffffffffff,%rbx
cmp    %ebx,%r15d
jne    1180 <rtrim+0x50>
jmp    1145 <rtrim+0x15>

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

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