#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(int);
int strlen(char *);
char *swi_right_trim(char *s) {
  char *sp = s + strlen(s) - 1;
  while (isspace((int)(unsigned char)*sp) && sp >= s)
    *sp-- = '\0';
  return s;
}


x86 assembly (clang, -O0):
<swi_right_trim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
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),%rax
movzbl (%rax),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     117d <swi_right_trim+0x5d>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
setae  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118d <swi_right_trim+0x6d>
jmp    11a4 <swi_right_trim+0x84>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
jmp    1151 <swi_right_trim+0x31>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swi_right_trim>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
movslq %eax,%rbx
lea    (%r14,%rbx,1),%r15
add    $0xffffffffffffffff,%r15
movzbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
cmp    %r14,%r15
jb     117a <swi_right_trim+0x5a>
test   %rax,%rax
je     117a <swi_right_trim+0x5a>
add    %r14,%rbx
add    $0xfffffffffffffffe,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movb   $0x0,0x1(%rbx)
movzbl (%rbx),%edi
call   1040 <isspace@plt>
cmp    %r14,%rbx
jb     117a <swi_right_trim+0x5a>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1160 <swi_right_trim+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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