#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 *rtrim(char *s) {
  register char *end;
  register int len;
  len = strlen(s);
  while (*s && len) {
    end = s + (len - 1);
    if (isspace(*end))
      *end = 0;
    else
      break;
    len = strlen(s);
  }
  return s;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
je     1157 <rtrim+0x37>
cmpl   $0x0,-0x14(%rbp)
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1167 <rtrim+0x47>
jmp    11b3 <rtrim+0x93>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     119d <rtrim+0x7d>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    11a2 <rtrim+0x82>
jmp    11b3 <rtrim+0x93>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
jmp    1138 <rtrim+0x18>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
nopw   0x0(%rax,%rax,1)
call   1030 <strlen@plt>
cmpb   $0x0,(%r14)
je     115d <rtrim+0x3d>
test   %eax,%eax
je     115d <rtrim+0x3d>
movslq %eax,%rbx
movsbl -0x1(%rbx,%r14,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     115d <rtrim+0x3d>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %r14,%rdi
jmp    1130 <rtrim+0x10>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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