#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 *strtrimr(char *str) {
  int i = strlen(str) - 1;
  while (isspace((int)str[i]) && (i >= 0))
    str[i--] = 0;
  return str;
}


x86 assembly (clang, -O0):
<strtrimr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xd(%rbp)
je     1168 <strtrimr+0x48>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1178 <strtrimr+0x58>
jmp    1193 <strtrimr+0x73>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0xffffffff,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    113b <strtrimr+0x1b>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtrimr>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebp
movslq %eax,%rbx
movsbl -0x1(%rbx,%r14,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1182 <strtrimr+0x62>
test   %ebp,%ebp
jle    1182 <strtrimr+0x62>
lea    (%rbx,%r14,1),%rax
add    $0xffffffffffffffff,%rax
mov    %ebp,%ebp
lea    -0x2(%r14),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movb   $0x0,(%rax)
movsbl (%rbx,%rbp,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     1182 <strtrimr+0x62>
lea    (%rbx,%rbp,1),%rax
lea    -0x1(%rbp),%rcx
cmp    $0x1,%rbp
mov    %rcx,%rbp
jne    1160 <strtrimr+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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