#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 *);
int eraseTrailingSpaces(char *s) {
  int len = strlen(s);
  int i;
  char c;
  int erased = 0;
  for (i = len - 1; i >= 0; --i) {
    c = s[i];
    if (isspace(c)) {
      s[i] = 0;
      ++erased;
    } else
      break;
  }
  return erased;
}


x86 assembly (clang, -O0):
<eraseTrailingSpaces>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11a5 <eraseTrailingSpaces+0x85>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%edi
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     118d <eraseTrailingSpaces+0x6d>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1192 <eraseTrailingSpaces+0x72>
jmp    11a5 <eraseTrailingSpaces+0x85>
jmp    1197 <eraseTrailingSpaces+0x77>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    1148 <eraseTrailingSpaces+0x28>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eraseTrailingSpaces>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
test   %eax,%eax
jle    1168 <eraseTrailingSpaces+0x48>
mov    %eax,%r14d
mov    %eax,%ebx
add    $0xffffffffffffffff,%rbx
xor    %r12d,%r12d
mov    %ebx,%ebp
movsbl (%r15,%rbp,1),%edi
call   1040 <isspace@plt>
test   %rax,%rax
je     116b <eraseTrailingSpaces+0x4b>
movb   $0x0,(%r15,%rbp,1)
add    $0x1,%r12d
add    $0xffffffffffffffff,%rbx
cmp    $0xffffffff,%ebx
jne    1140 <eraseTrailingSpaces+0x20>
mov    %r14d,%r12d
jmp    116b <eraseTrailingSpaces+0x4b>
xor    %r12d,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @eraseTrailingSpaces(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %18
4: ; preds = %1
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %4, %15
%7 = phi i64 [ %5, %4 ], [ %9, %15 ]
%8 = phi i32 [ 0, %4 ], [ %16, %15 ]
%9 = add nsw i64 %7, -1
%10 = and i64 %9, 4294967295
%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 %18, label %15
15: ; preds = %6
store i8 0, i8* %11
%16 = add nuw nsw i32 %8, 1
%17 = icmp eq i32 %16, %2
br i1 %17, label %18, label %6
18: ; preds = %15, %6, %1
%19 = phi i32 [ 0, %1 ], [ %8, %6 ], [ %2, %15 ]
ret i32 %19
}