#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 *);
void rtrim(char *s) {
  char *tmp;
  if (s) {
    tmp = s + strlen(s) - 1;
    while (tmp >= s && isspace(*tmp))
      --tmp;
    if (tmp > s)
      *++tmp = 0;
  }
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11cc <rtrim+0xac>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    -0x8(%rbp),%rcx
mov    %al,-0x19(%rbp)
jb     1185 <rtrim+0x65>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1195 <rtrim+0x75>
jmp    11a6 <rtrim+0x86>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    115c <rtrim+0x3c>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jbe    11c7 <rtrim+0xa7>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,0x1(%rax)
jmp    11cc <rtrim+0xac>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1162 <rtrim+0x42>
mov    %rdi,%r14
call   1030 <strlen@plt>
cltq
lea    (%rax,%r14,1),%rcx
add    $0xffffffffffffffff,%rcx
nopl   0x0(%rax,%rax,1)
mov    %rcx,%rbx
cmp    %r14,%rcx
jb     1159 <rtrim+0x39>
movsbl (%rbx),%edi
call   1040 <isspace@plt>
lea    -0x1(%rbx),%rcx
test   %rax,%rax
jne    1140 <rtrim+0x20>
cmp    %r14,%rbx
jbe    1162 <rtrim+0x42>
movb   $0x0,0x1(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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