#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 *);
void rtrim(char *string) {
  int len;
  if (string == NULL) {
    return;
  }
  len = strlen(string) - 1;
  while (isspace(*(string + len)) && (len != 0)) {
    len--;
  }
  *(string + len + 1) = 0;
  return;
}


x86 assembly (clang, -O0):
<rtrim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    113c <rtrim+0x1c>
jmp    11a3 <rtrim+0x83>
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     1178 <rtrim+0x58>
cmpl   $0x0,-0xc(%rbp)
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1188 <rtrim+0x68>
jmp    1196 <rtrim+0x76>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    114b <rtrim+0x2b>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtrim>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     1175 <rtrim+0x55>
mov    %rdi,%r14
call   1030 <strlen@plt>
movslq %eax,%r15
lea    (%r15,%r14,1),%r12
add    $0x1,%r12
mov    $0x1,%r13d
sub    %r15d,%r13d
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl -0x2(%r12,%rbx,1),%edi
call   1040 <isspace@plt>
lea    -0x1(%rbx),%rcx
test   %rax,%rax
je     116c <rtrim+0x4c>
cmp    %ebx,%r13d
mov    %rcx,%rbx
jne    1150 <rtrim+0x30>
add    %r14,%r15
movb   $0x0,0x1(%rcx,%r15,1)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @rtrim(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %18, label %3
3: ; preds = %1
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
br label %6
6: ; preds = %6, %3
%7 = phi i64 [ %8, %6 ], [ %5, %3 ]
%8 = add nsw i64 %7, -1
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = tail call i64 @isspace(i8 signext %10)
%12 = icmp ne i64 %11, 0
%13 = trunc i64 %8 to i32
%14 = icmp ne i32 %13, 0
%15 = select i1 %12, i1 %14, i1 false
br i1 %15, label %6, label %16
16: ; preds = %6
%17 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 0, i8* %17
br label %18
18: ; preds = %1, %16
ret void
}