#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 *RmTrailingSpaces(char *str) {
  if (str) {
    int i;
    for (i = strlen(str) - 1; i >= 0 && isspace(str[i]); --i)
      ;
    str[++i] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<RmTrailingSpaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a7 <RmTrailingSpaces+0x87>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jl     1170 <RmTrailingSpaces+0x50>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1180 <RmTrailingSpaces+0x60>
jmp    1193 <RmTrailingSpaces+0x73>
jmp    1185 <RmTrailingSpaces+0x65>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1146 <RmTrailingSpaces+0x26>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RmTrailingSpaces>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     1165 <RmTrailingSpaces+0x45>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
xor    %ebp,%ebp
test   %eax,%eax
cmovs  %eax,%ebp
nopl   (%rax)
test   %ebx,%ebx
jle    115d <RmTrailingSpaces+0x3d>
movsbl -0x1(%r14,%rbx,1),%edi
call   1040 <isspace@plt>
add    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1140 <RmTrailingSpaces+0x20>
add    $0x1,%ebx
mov    %ebx,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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