#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(char);
int strlen(char *);
char *xstrdup(char const *);
char *rtrim(const char *str) {
  int i;
  char *copy;
  char *tail;
  copy = xstrdup(str);
  for (tail = copy + strlen(copy) - 1;
       (unsigned long)copy <= (unsigned long)tail; tail--) {
    if (!isspace(*tail))
      break;
    *tail = '\0';
  }
  return copy;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <xstrdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
cmp    %rcx,%rax
ja     11b1 <rtrim+0x81>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%eax
jne    1199 <rtrim+0x69>
jmp    11b1 <rtrim+0x81>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    116e <rtrim+0x3e>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r14
push   %rbx
push   %rax
call   1040 <xstrdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
cltq
lea    (%r14,%rax,1),%rbx
add    $0xffffffffffffffff,%rbx
cmp    %rbx,%r14
ja     1178 <rtrim+0x48>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%rbx),%edi
call   1050 <isspace@plt>
test   %eax,%eax
je     1178 <rtrim+0x48>
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
cmp    %rbx,%r14
jbe    1160 <rtrim+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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