#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 */
size_t strlen(char *);
char *xTrimRight(char *buffer) {
  size_t i;
  i = strlen(buffer);
  if (i == 0)
    return (buffer);
  do {
    i--;
    if (buffer[i] > '\0' && buffer[i] <= ' ')
      buffer[i] = '\0';
    else
      break;
  } while (i > 0);
  return (buffer);
}


x86 assembly (clang, -O0):
<xTrimRight>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1141 <xTrimRight+0x31>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11aa <xTrimRight+0x9a>
jmp    1146 <xTrimRight+0x36>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jle    118d <xTrimRight+0x7d>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jg     118d <xTrimRight+0x7d>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1192 <xTrimRight+0x82>
jmp    11a2 <xTrimRight+0x92>
jmp    1197 <xTrimRight+0x87>
cmpq   $0x0,-0x18(%rbp)
ja     1146 <xTrimRight+0x36>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xTrimRight>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %rax,%rax
je     1138 <xTrimRight+0x28>
xchg   %ax,%ax
movzbl -0x1(%rbx,%rax,1),%ecx
add    $0xff,%cl
cmp    $0x1f,%cl
ja     1138 <xTrimRight+0x28>
movb   $0x0,-0x1(%rbx,%rax,1)
add    $0xffffffffffffffff,%rax
jne    1120 <xTrimRight+0x10>
mov    %rbx,%rax
pop    %rbx
ret

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

LLVM IR (-O2):
define i8* @xTrimRight(i8* returned %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %1, %11
%5 = phi i64 [ %6, %11 ], [ %2, %1 ]
%6 = add i64 %5, -1
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = add i8 %8, -1
%10 = icmp ult i8 %9, 32
br i1 %10, label %11, label %13
11: ; preds = %4
store i8 0, i8* %7
%12 = icmp eq i64 %6, 0
br i1 %12, label %13, label %4
13: ; preds = %11, %4, %1
ret i8* %0
}